Nui.define(['../components/layer/layerExt', 'util'], function(layer, util){
    var ajax = $.ajax;

    var request = function(options, text, under){
        if(typeof options === 'string'){
            options = {
                url:options,
                type:'GET',
                dataType:'json'
            }
        }

        var loading;
        
        if(text !== null){
            var opts = {
                content:text||'正在加载数据...'
            }
            if(under){
                opts.under = under
            }
            loading = layer.loading(opts)
        }

        var success = options.success || $.noop;
        var error = options.error || $.noop;

        //登录拦截器
        var intercept = function(){
            if(typeof request.intercept === 'function'){
                request.intercept.apply(this, arguments)
            }
        }

        options.success = function(res, status, xhr){
            if(res){
                intercept.call(this, res, status, xhr)
            }

            if(loading){
                loading.destroy()
            }
            
            success.call(this, res, status, xhr)
        }

        options.error = function(xhr){
            if(loading){
                loading.destroy()
            }
            if(xhr.status !== 200 && xhr.status >= 400){
                layer.message('error', '请求状态码：'+xhr.status+'，请重试或者联系管理员！')
            }
            error.apply(this, arguments)
        }

        if(options.url && !/^https?:\/\//.test(options.url) && Nui.domain){
            options.url = Nui.domain+options.url;
        }

        if(options.cache !== true){
            options.url = util.setParam('_', new Date().getTime(), options.url);
            delete options.cache
        }
        
        return ajax($.extend(true, {
            dataType:'json'
        }, options))
    }

    request.intercept = null;

    var method = function(options, message){
        return function(url, data, callback, text, under){

            if(typeof data === 'function'){
                under = text;
                text = callback;
                callback = data;
                data = undefined;
            }
            else if(typeof data === 'string' || data === null){
                under = callback;
                text = data;
                data = callback = undefined;
            }

            if(typeof callback === 'object'){
                if(callback === null){
                    under = text;
                    text = callback;
                    callback = undefined;
                }
                else{
                    var object = callback;
                    callback = function(res, status, xhr){
                        //老项目是result 新项目是status
                        if(res && (res.result || res.status)){
                            Nui.each(object, function(_callback, result){
                                if(
                                    (result === 'other' && res.result !== 'success' && res.status != 200) || 
                                    res.result === result || 
                                    res.status == result
                                ){
                                    _callback.call(object, res, status, xhr)
                                    return false
                                }
                            })
                        }
                    }
                }
            }

            if(text && typeof text === 'object'){
                under = text;
                text = undefined;
            }

            if(message && !text && text !== null){
                text = message
            }

            return request($.extend({
                url:url,
                data:data,
                success:callback
            }, options), text, under)
        }
    }

    request.get = method({
        type:'GET'
    });

    request.sync = method({
        type:'GET',
        async:false
    });

    request.update = method({
        type:'GET'
    }, '正在保存数据...');

    request.jsonp = method({
        type:'GET',
        dataType:'jsonp'
    });

    request.del = method({
        type:'DELETE'
    }, '正在删除数据...');

    request.put = method({
        type:'PUT'
    }, '正在保存数据...');

    request.post = method({
        type:'POST'
    });

    request.postSync = method({
        type:'POST',
        async:false
    });

    request.postUpdate = method({
        type:'POST'
    }, '正在保存数据...');

    return $.ajax = request
})